package pt.ua.helpdesk.server;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import persistence.Administrator;
import persistence.Assignment;
import persistence.Assistant;
import persistence.Command;
import persistence.Comment;
import persistence.Faq;
import persistence.Person;
import pt.ua.helpdesk.client.ServiceAdministration;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.jscape.inet.util.MD5;

public class ServiceAdministrationImpl extends RemoteServiceServlet implements ServiceAdministration {

	public String addUser(String username, String pass, String Email, String name, String type)
	{
		String ret = "";
    	
    	// Verificar dados unique
        // Parametros da query
        ArrayList<String> par = new ArrayList<String>();
        par.add(Email);
        par.add(username);
    	List<Person> l = (List<Person>)Command.select("select p from Person p where (p.email LIKE ?1) OR (p.username LIKE ?2)", par);

    	if (l.size() != 0) {
    		Person p = l.get(0);
    		if(p.getEmail().equals(Email))
    			ret = "1";
    		if (p.getUsername().equals(username))
    			ret += "2";
    	}
    	else
    	{
	        boolean flag = false;

	        if ( type.equals("Assistente") )
	        	flag = createAssistant(username, pass, Email, name);
	        else
	        {
	        	if ( type.equals("Administrador") )
	        		flag = createAdmin(username, pass, Email, name);	
	        	else
	        		System.out.println("Tipo de Utilizador não Reconhecido -- " + type);
	        }
	        
	        if(flag)
	        	ret = "";
	        else
	        	ret = "9";
    	}
		return ret ;
	}

	public boolean removeUser(Integer idUser)
	{	
		System.out.println("step1");
		Person p = (Person) Command.find(Person.class, idUser);
		System.out.println("step2");
		p.setActive((short)0);
		System.out.println("step3");
		return new Boolean(Command.update(p));
	}

	public ArrayList searchUser(String name, String username, String mail, String usertype)
	{
		ArrayList<Object> idUser = new ArrayList<Object>() ;
		ArrayList<Object> unameUser = new ArrayList<Object>() ;
		ArrayList<Object> typeUser = new ArrayList<Object>() ;
		ArrayList<Object> nameUser = new ArrayList<Object>() ;
		ArrayList<Object> mailUser = new ArrayList<Object>() ;
		ArrayList<ArrayList> ret = new ArrayList<ArrayList>() ; 
		
		ArrayList<String> param = new ArrayList<String>() ;
		String sql = "select p from Person p where (p.active = 1) " ;
		
		if ( !name.equals(""))
		{
			param.add("%"+name+"%");
			sql += "AND (p.name LIKE ?" + param.size() + ")" ;
		}
		
		if ( !username.equals(""))
		{
			param.add("%"+username+"%");
			sql += "AND (p.username LIKE ?" + param.size() + ")" ;
		}
		
		if ( !mail.equals(""))
		{
			param.add("%"+mail+"%");
			sql += "AND (p.email LIKE ?" + param.size() + ")" ;
		}
		
		if (!usertype.equals("Todos"))
		{
			param.add(usertype);
			sql += "AND (p.PersonType LIKE ?" + param.size() + ")" ;
		}
		
		System.out.println(sql);
		
		List<Person> l = (List<Person>) Command.select(sql, param);
		
		for (int i = 0; i < l.size(); ++i)
		{
			Person p = (Person)l.get(i);
			
			idUser.add(p.getIdperson());
			unameUser.add(p.getUsername());
			if (p.getPersonType().equals("Administrador"))
			{
				Administrator a = (Administrator)Command.find(Administrator.class, p.getIdperson());
				if (a.getLevel() == (short)1)
					typeUser.add("Administrador");
				else
					typeUser.add("CEO");
			}	
			else
				typeUser.add(p.getPersonType());
			
			nameUser.add(p.getName());
			mailUser.add(p.getEmail());
		}
		
		ret.add(idUser) ;
		ret.add(unameUser);
		ret.add(typeUser) ;
		ret.add(nameUser);
		ret.add(mailUser);
		
		return ret ;
	}
	
	private boolean createAssistant(String username, String pass, String Email, String name)
	{
		Date now = new Date();
		MD5 md5 = new MD5(pass);
		Assistant a = new Assistant() ;
		
		a.setActive((short)1);
        a.setDate(now);
        a.setEmail(Email);
        a.setName(name);
        a.setPassword(md5.toString());
        a.setUsername(username);
        a.setCommentCollection(new ArrayList<Comment>());
        a.setPersonType("Assistente");
        a.setAssignmentCollection(new ArrayList<Assignment>()) ;
    	a.setFaqCollection(new ArrayList<Faq>());
		
		return Command.set(a);
	}
	
	private boolean createAdmin(String username, String pass, String Email, String name)
	{
		Date now = new Date();
		MD5 md5 = new MD5(pass);
		Administrator a = new Administrator() ;
		
		a.setActive((short)1);
        a.setDate(now);
        a.setEmail(Email);
        a.setName(name);
        a.setPassword(md5.toString());
        a.setUsername(username);
        a.setAssignmentCollection(new ArrayList<Assignment>()) ;
    	a.setRefidfaqCollection(new ArrayList<Faq>());
		a.setLevel((short)1);
		a.setPersonType("Administrador");
		
		return Command.set(a);
	}
}
